package com.xj.api.bussiness.wxLogin.web;

import com.alibaba.fastjson.JSON;
import com.xj.api.base.web.BaseController;
import com.xj.api.bussiness.wxLogin.common.TemplateData;
import com.xj.api.bussiness.wxLogin.common.WxMessageModel;
import com.xj.api.bussiness.wxLogin.service.IWxMessageService;
import com.xj.common.base.common.bean.AbstractBean;
import com.xj.common.base.common.util.HttpRequest;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("wxMessage")
public class wxMessageController extends BaseController {
    @Autowired
    private IWxMessageService wxMessageService;

    @PostMapping("/pushMessage")
    @ApiOperation(value = " 模板消息推送", httpMethod = "POST", notes = "  模板消息推送")
    public AbstractBean pushMessage(WxMessageModel wxMessageModel, String content){
        Map<String,TemplateData> map = wxMessageService.getTempalteDat(content);
        String url = wxMessageService.getMessagePushUrl();
        if(url != null && url != "" && map!= null){
            wxMessageModel.setData(map);
            String jsonModel = JSON.toJSONString(wxMessageModel);
            //发送模板消息请求到微信服务端
            String resposeContent = HttpRequest.httpRequest(url,"POST",jsonModel);
            Object resposeJson = JSON.parse(resposeContent);
            return json(resposeJson);
        }
        return error();
    }
}
